<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            margin: 0;padding: 0;
            list-style: none;
        }
        #app{
            margin: 50px auto;
        }
        #father{
            color: green;
        }
        #son{
            position: fixed;
            background-color: rgba(0, 0, 0, .1);
            width: 100%;height: 100%;
            top: 0;left: 0;
            
        }
        #son_content{
            width: 200px;
            height: 150px;
            top: calc( 50% - 75px);
            left: calc( 50% - 100px);
            border: 1px solid red;
            position: absolute;
            background-color: #fff;
            color: #000;
        }
        #delete{
            background-color: red;
            border: none;
        }
    </style>
</head>
<body>
    <!-- 子组件向父组件传递数据 -->
    <!-- 1. 通过this.$emit 自定义事件 -->
    <!-- 2.  -->
    
    <div id="app">
        <my-father></my-father>
    </div>

     <!-- 父组件 -->
     <template id="father">
        <div id="father">
            <h2>
                我是父组件
            </h2>
            <my-son @send="acceptStr" v-show="str"></my-son>
            <button @click="change">点击显示子组件</button>
        </div>
    </template>

    <!-- 子组件 -->
    <template id="son">
        <div id="son">
            <div id="son_content">
                <h4>
                    我是子组件
                </h4>
                <button @click="change" id="delete">X</button>
            </div>
        </div>
    </template>

    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
    <script>
       
       let Son = {
            template: "#son",
            data() {
                return {
                    str: false
                }
            },
            methods: {
                sendStr() {
                    // this.$emit(事件名称, 要传递的内容)
                    this.$emit("send", this.str)
                },
                change() {
                    this.str = this.str ? false : true;
                    this.$emit("send", this.str)
                }
            },
            mounted () {
                this.sendStr()
            }
        }



        let Father = {
            template: "#father",
            components: {
                "my-son": Son
            },
            data() {
                return {
                    str: false
                }
            },
            methods: {
                acceptStr(data) {
                    this.str = data
                },
                change(){
                    this.str = this.str ? false : true;
                }
            }
        }


        new Vue({
            // 元素
            el: "#app",
            components: {
                "my-father": Father
            }
        })
    </script>
</body>
</html>